package com.api.weavernorth.salary;


import com.weavernorth.createDoc.ToCreateDoc;
import com.weavernorth.salary.GetV8MessageUtil;
import com.weavernorth.salary.MessForV8Vo;
import com.weavernorth.sysap.util.SyncUtil;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ecology_erdos
 * @description: 单个店铺获取v8数据
 * @author: zm
 * @create: 2020-05-01
 **/

@Path("/weavernorth/salaryparm")
public class GetV8MessForParam extends HttpServlet {
//    public static Log log = LogFactory.getLog(GetV8MessForParam.class.getName());
    Logger log = Logger4jUtil.getLoggerByName("/action/api/salary/GetV8MessForParam/");
    @GET
    @Path("/getV8messforparam")
    @Produces(MediaType.TEXT_PLAIN)
    public void getV8Message(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String strDpbh=Util.null2String(request.getParameter("dpbh"));
        String strDate=Util.null2String(request.getParameter("fqsj"));
        log.info("获取单个店铺的编号:"+strDpbh+",日期:"+strDate);
        //执行存储过程类
        GetV8MessageUtil getV8MessageUtil = new GetV8MessageUtil();
        //获取员工信息类
        SyncUtil syncUtil = new SyncUtil();
        String strFormid = syncUtil.getModeIdBytableName("uf_xsyjhzb");
//        //获取当前年月
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(new Date()); //设置时间为当前时间
//        cal.add(Calendar.MONTH, -1);
//        String preMonth=format.format(cal.getTime());
        String year = "";
        String month = "";
        if (!"".equals(strDate)) {
            year = strDate.split("-")[0];
            month = strDate.split("-")[1];
            log.info("当前年:" + year + ",月:" + month);
        }
        //年月
        String strYearMonth = String.valueOf(year) + String.valueOf(month);
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsCs = new RecordSet();
        RecordSet rsDelete = new RecordSet();
        String strDpid="";
        //查询当前的店铺信息
        String sql = "select id,dpsapid ,dp from uf_dpxx where dpsapid=?";
        log.info("获取当前店铺信息sql:" + sql);
        rs.executeQuery(sql, strDpbh);
        if (rs.next()) {
            //Sap店铺id
            String strDpSapid = Util.null2String(rs.getString("dpsapid"));
           // 当前店铺id
             strDpid = Util.null2String(rs.getString("dp"));

    }
            try {
                log.info("获取店铺sap编号:" + strDpbh);
                //执行存储过程,获取V8数据
                List<MessForV8Vo> listMess = getV8MessageUtil.getV8Mess(String.valueOf(year), String.valueOf(month), strDpbh, strDpid, rsCs);
                log.info("v8返回数据长度:" + (listMess.size()));
                log.info("v8返回数据:"+listMess.toString());
                //插入建模表
                //删除重新获取
                String deleteSql = "delete from uf_xsyjhzb where dpbm=? and xzrq=?";
                log.info("删除语句:"+deleteSql+",strDpbh:"+strDpbh+",strYearMonth:"+strYearMonth);
                boolean flag = rsDelete.executeUpdate(deleteSql, strDpbh,strYearMonth);
                //删除结束重新插入
                if (listMess.size() > 0 && flag) {
                    getV8MessageUtil.insertV8Mess(strFormid, strYearMonth, listMess, rsCs);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
}









